package cn.itcast.job.service.Impl;

import cn.itcast.job.dao.JobInfoDao;
import cn.itcast.job.pojo.JobInfo;
import cn.itcast.job.service.JobInfoService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
public class JobInfoServiceImpl implements JobInfoService {


    @Autowired
    private JobInfoDao jobInfoDao;

    @Override
    @Transactional
    public void save(JobInfo jobInfo) {
//        查询信息，根据url和时间
        JobInfo param = new JobInfo();
        param.setUrl(jobInfo.getUrl());
        param.setTime(jobInfo.getTime());

        List<JobInfo> lis = this.findJobInfo(param);

//        判断数据库中是否有已存在的数据，
        if (lis.size() == 0) {

// 不存在执行新增// 如果已存在执行更新，
    this.jobInfoDao.saveAndFlush(jobInfo);
        }



    }

    @Override
    public List<JobInfo> findJobInfo(JobInfo jobInfo) {

//        设置查询条件

        Example example = Example.of(jobInfo);
//执行查询
        List list = this.jobInfoDao.findAll(example);
        return list;
    }
}
